package labox.innovation.gameserver.ai;

import static labox.innovation.gameserver.ai.CtrlIntention.AI_INTENTION_ACTIVE;
import static labox.innovation.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
import static labox.innovation.gameserver.ai.CtrlIntention.AI_INTENTION_FOLLOW;
import static labox.innovation.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;

import labox.innovation.gameserver.model.actor.L2Summon;
import labox.innovation.gameserver.model.actor.L2Character.AIAccessor;
import labox.innovation.gameserver.util.Util;

/**
 * 此类管理了所有召唤物的AI逻辑
 * 
 * @author Administrator
 * 
 */
public class L2SummonAI extends L2CharacterAI {

	private boolean _startFollow = ((L2Summon) _actor).getFollowStatus();

	public L2SummonAI(AIAccessor accessor) {
		super(accessor);
	}

	@Override
	protected void onIntentionIdle() {
		_startFollow = false;
		onIntentionActive();
	}

	@Override
	protected void onIntentionActive() {
		L2Summon summon = (L2Summon) _actor;
		if (_startFollow)
			setIntention(AI_INTENTION_FOLLOW, summon.getOwner());
		else
			super.onIntentionActive();
	}

	private void thinkAttack() {
		if (checkTargetLostOrDead(getAttackTarget())) {
			// 设置意图为AI_INTENTION_ACTIVE
			setIntention(AI_INTENTION_ACTIVE);
			setAttackTarget(null);
			return;
		}

		if (Util.FastDistance2D(_attackTarget.getX(), _attackTarget.getY(), _actor.getX(), _actor.getY()) > _actor.getPhysicalAttackRange()) {
			setIntention(AI_INTENTION_FOLLOW, _attackTarget);
			return;
		}

		_accessor.doAttack(getAttackTarget());
	}

	private void thinkCast() {
		L2Summon summon = (L2Summon) _actor;
		if (checkTargetLost(getCastTarget())) {
			setCastTarget(null);
			return;
		}

		if (Util.FastDistance2D(_castTarget.getX(), _castTarget.getY(), _actor.getX(), _actor.getY()) > _actor.getMagicalAttackRange(_skill)) {
			setIntention(AI_INTENTION_FOLLOW, _castTarget);
			return;
		}

		summon.setFollowStatus(false);
		_accessor.doCast(_skill);
	}

	private void thinkPickUp() {
		if (checkTargetLost(getTarget()))
			return;

		if (Util.FastDistance2D(_target.getX(), _target.getY(), _actor.getX(), _actor.getY()) > 20) {
			setIntention(AI_INTENTION_FOLLOW, _target);
			return;
		}
		((L2Summon.AIAccessor) _accessor).doPickupItem(getTarget());
		setIntention(AI_INTENTION_IDLE);
	}

	private void thinkInteract() {
		if (checkTargetLost(getTarget()))
			return;
		if (Util.FastDistance2D(_target.getX(), _target.getY(), _actor.getX(), _actor.getY()) > 20) {
			setIntention(AI_INTENTION_FOLLOW, _target);
			return;
		}
		setIntention(AI_INTENTION_IDLE);
	}

	@Override
	protected void onEvtThink() {
		if (_actor.isAllSkillsDisabled())
			return;

		try {
			switch (getIntention()) {
			case AI_INTENTION_ATTACK:
				thinkAttack();
				break;
			case AI_INTENTION_CAST:
				thinkCast();
				break;
			case AI_INTENTION_PICK_UP:
				thinkPickUp();
				break;
			case AI_INTENTION_INTERACT:
				thinkInteract();
				break;
			default:
				super.onEvtThink();
				break;
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	protected void onEvtFinishCasting() {
		if (_actor.getAI().getIntention() != AI_INTENTION_ATTACK)
			((L2Summon) _actor).setFollowStatus(_startFollow);
	}

	public void notifyFollowStatusChange() {
		_startFollow = !_startFollow;
		switch (getIntention()) {
		case AI_INTENTION_ACTIVE:
		case AI_INTENTION_FOLLOW:
		case AI_INTENTION_IDLE:
			((L2Summon) _actor).setFollowStatus(_startFollow);
		}
	}

	public void setStartFollowController(boolean val) {
		_startFollow = val;
	}
}
